const defaultState = {
    visible: false,
    loading: false,
    model: {}

};
export default function create(state = defaultState, action) {
    const {type, error, payload} = action;
    switch (type) {
        case 'CLIENT.SHOW_CREATE':
            return {...state, visible: true, model: {"rates": [], "contract": {}}};
        case 'CLIENT.HIDE_CREATE':
            return {...state, visible: false, model: {"rates": [], "contract": {}}};
        case 'CLIENT.UPDATE_CLIENT_MODEL':
            return {...state, model: {...payload}};
        case 'CLIENT.CREATE':
            if (error === true) {
                return {...state, loading: false};
            }
            return {...state, loading: false, visible: false};
        case 'CLIENT.CREATE_PENDING':
            return {...state, loading: true, model: {...payload}};
        case 'CLIENT.GET_COMMON_RATE':
            const model = state.model;
            console.info('model', model);
            if (payload) {
                payload.forEach((item, i) => {
                    const found = model.rates.find(rate => rate.fromRate === item.fromRate && rate.toRate === item.toRate);
                    if (!found) {
                        model.rates.push(item);
                    }
                });
            }
            return {...state, model: {...model}};
        default:
            return state;
    }
}

